<?php
namespace Home\Controller;
use Think\Controller;
class OrderController extends Controller {
	//展现购物车
    public function checkout(){
    	$Tool =\Home\Tool\AddTool::getIns();
        $car = session("kache");
        $this->assign("totalPrice",$Tool->calcMoney()); 
        $this->assign("MPrice",$Tool->marketMoney());
        $this->assign("cha",$Tool->chaPrice());
        $this->assign("car",$car);
		$this->display();
	}
	//正式下单
	public function done()
	{
		// 实例化购物车类
		$car = \Home\Tool\AddTool::getIns();
		$ord = M('ordinfo');
		$ord->create();//收取post数据
		$ord->ord_sn = $ord_sn = date('Ymd').mt_rand(1000,9999);
		$ord->user_id = cookie("user_id")?cookie("useri_d") : 0;
		$ord->money = $moeny = $car->calcMoney();
		$ord->ordtime = time();
		if($ordinfo_id = $ord->add()){
			//添加到ordgoods表
			$ordgoods = M('ordgoods');
			$date = array();
			foreach($car->items() as $v)
			{
                $row = array();
                $row['goods_name'] = $v['goods_name'];
                $row['shop_price'] = $v['shop_price'];
                $row['ordinfo_id'] = $ordinfo_id;
                $row['num'] = $v['shop_price'];
             $data[] = $row;
			}
			if($ordgoods->addAll($data)){
				$this->assign("ord_sn",$ord_sn);
				$this->assign("moeny",$moeny);
				//在线支付
				$Jdpay = new \Home\Pay\JdPay($ord_sn,$moeny);
				$this->assign('from', $Jdpay->from());
				$car->clear();
				$this->display();
			}
		}else{
			echo "error";
		}
		
	}
}